<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./lib/react.development.js"></script>
    <script src="./lib/react-dom.development.js"></script>
    <script src="./lib/babel.min.js"></script>
</head>

<body>
    <div id="root"></div>
    <script type="text/babel">
        class App extends React.Component {
            state = {
                userName:"zhangpeiyue",
                age:12,
                sex:"男"
            }
            render(){
                return(
                    <div>
                        <button onClick={()=>{this.setState({age:this.state.age+1})}}>更新年龄</button>    
                        <button onClick={()=>{this.setState({sex:'女'})}}>更改性别</button>   
                        <A sex={this.state.sex} userName={this.state.userName}></A> 
                        <B age={this.state.age}></B> 
                    </div>
                )
            }
        }
    // PureComponent:纯组件（只有状态与属性发生改变时才会执行render）
        class A extends React.Component{
            state = {
                num:1
            }
        // shouldComponentUpdate(nextProps,nextState){
        //     // this.props:{userName:"zhangsan",sex:"男"}
        //     // nextProps:{userName:"zhangsan",sex:"男"}
        //     for(let key in nextProps){
        //         console.log(key,nextProps[key],this.props[key])
        //         if(nextProps[key] !== this.props[key]){
        //             return true;
        //         }
        //     }
        //
        //     for(let key in nextState){
        //         if(nextState[key] !== this.state[key]){
        //             return true;
        //         }
        //     }
        //     return false;
        // }
            render(){
                console.log("a->render");
                return (
                    <div>
                        <h3>A</h3>
                        <button onClick={()=>{
                            this.setState({
                                num:100
                            })
                        }}>{this.state.num}</button>
                        <p>userName:{this.props.userName}</p>
                        <p>sex:{this.props.sex}</p>
                    </div>
                )
            }
        }
        class B extends React.Component{
        render(){
            console.log("b->render");
            return (
                <div>
                    <h3>B</h3>
                    <p>age:{this.props.age}</p>
                </div>
            )
        }
    }
    ReactDOM.createRoot(document.querySelector("#root")).render(<App/>)

    </script>
</body>

</html>